<?php
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Traits\HasUuid;

class ConfigModel extends Model
{
    use HasFactory, HasUuid, SoftDeletes;

    protected $table = 'configs';
	
	protected $hidden = ["deleted_at"];

    protected $fillable = [];

    /**
     * 获取所有的配置
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getAll(): array
    {
        $group = ConfigGroupModel::query()
            ->where("is_hid",0)
            ->selectRaw("id,title,tab_name")
            ->orderBy("sort","desc")
            ->get()->toArray();

        foreach ($group as $keys=>$item) {
            $data = $this->newQuery()->where("is_hid", 0)
                ->where("group_id",$item['id'])
                ->orderBy('sort','desc')
                ->get()
                ->toArray();
            foreach ($data as $key => $vo) {
                if (!empty($vo['options']) && $vo['type'] == 'radio') {
                    $o = explode(";", $vo['options']);
                    $option = [];
                    foreach ($o as $v) {
                        $oo = explode(":", $v);
                        $t = [
                            'key' => $oo[1],
                            'value' => $oo[0]
                        ];
                        $option[] = $t;
                    }
                    $data[$key]['options'] = $option;
                }
            }
            $group[$keys]['data'] = $data;
        }
        $group['dataAll'] = $this->newQuery()
            ->where("is_hid", 0)
            ->orderBy("id", 'asc')
            ->get()
            ->toArray();
        return $group;
    }

}
